package com.ymh.spring.learn.aop.aspect;

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 
 * 名称：TrackCounter<br/>
 * 描述：用于记录磁道播放次数的切面<br/>
 * 创建时间：2018年3月23日 下午5:00:58 <br/>
 * 
 * @author aivin
 * @version 1.0.0
 */
@Aspect
public class TrackCounter {

	private Map<Integer, Integer> trackCounts = new HashMap<Integer, Integer>();

	/**
	 * 
	 * <h4>功能：通知play()方法</h4>
	 * 
	 * @param trackNum
	 */
	@Pointcut("execution(* com.ymh.spring.learn.aop.service.CompactDisc.play(int)) && args(trackNum)")
	public void trackPlayed(int trackNum) {

	}

	/**
	 * 
	 * <h4>功能：在播放前，为该磁道计数</h4>
	 * 
	 * @param trackNum
	 */
	@Before("trackPlayed(trackNum)")
	public void countTrack(int trackNum) {
		int currentCount = getPlayCount(trackNum);
		trackCounts.put(trackNum, currentCount + 1);
	}

	public int getPlayCount(int trackNum) {
		return trackCounts.containsKey(trackNum) ? trackCounts.get(trackNum)
				: 0;
	}
}
